Concise description?
Example section of code?
Trace of it?
Full programs that use loops?
In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. A while statement executes a block of code repeatedly, so the while loop can be thought of as a repeating if statement.
The while construct consists of a block of code and a condition. The condition is first evaluated - if the condition is true the code within the block is then executed. This repeats until the condition becomes false. Because while loops check the condition before the block is executed, the control structure is often also known as a pre-test loop. Compare with the do while loop, which tests the condition after the loop has executed.
For example:
x = 0;
while (x < 3)
{
x++; //same as x=x+1
}
the code fragment first checks whether x is larger than 3, which it is not, so it increments x by 1. It then checks the condition again, and executes again, repeating this process until the variable x has the value 3.
Note that it is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that controls termination of the loop.
While loops are used in order to execute command(s) as long as the condition given is still true. In the above example, the command Willy.setLocation ( Willy.getX() + 1, Willy.getY() ); will continue to happen until the condition, in this case, ( Willy.getX() < myWorld.getX() ) is still true. When the x-coordinate of Willy is greater or equal to myWorld.getX(), the loop is complete.
//Move a wombat Willy across the screenwhile( Willy.getX()< myWorld.getX()){//Not at the end yet, add one to the X value, keep Y the same
Willy.setLocation( Willy.getX()+1, Willy.getY());}
Example
public int swan()
{
int count = 10, blah = 10;
while(count>0)
{
count --; //count = count-1;
blah += count; //blah = blah + count;
}
return blah;
}
Explanation of ++, --, etc In Code: ++ (adds 1) -- (subtracts 1) += (adds count to blah) -= (subtracts from blah) *= (multiplies to blah) /= (divides by blah)
Code
Same as:
count++
count = count + 1
count--
count = count - 1
count += blah
count = count + blah
count -= blah
count = count - blah
count *= blah
count = count * blah
count /= blah
count = count / blah
Difference between loops & if statements:
-if statements repeat only ONCE
-loops keep going until it is false
-be very careful when using loops. If you are not, you can easily create an endless loop that will keep on working and never be false and could very easily crash your computer.
In a for or while loop, the condition is evaluated at the beginning of the loop and the program does not go inside the loop if the condition is false. Thus, the body of the loop may be skipped entirely if the condition is false at the very beginning.
Example section of code?
Trace of it?
Full programs that use loops?
In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. A while statement executes a block of code repeatedly, so the while loop can be thought of as a repeating if statement.
The while construct consists of a block of code and a condition. The condition is first evaluated - if the condition is true the code within the block is then executed. This repeats until the condition becomes false. Because while loops check the condition before the block is executed, the control structure is often also known as a pre-test loop. Compare with the do while loop, which tests the condition after the loop has executed.
For example:
x = 0; while (x < 3) { x++; //same as x=x+1 }the code fragment first checks whether x is larger than 3, which it is not, so it increments x by 1. It then checks the condition again, and executes again, repeating this process until the variable x has the value 3.Note that it is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that controls termination of the loop.
While loops are used in order to execute command(s) as long as the condition given is still true. In the above example, the command Willy.setLocation ( Willy.getX() + 1, Willy.getY() ); will continue to happen until the condition, in this case, ( Willy.getX() < myWorld.getX() ) is still true. When the x-coordinate of Willy is greater or equal to myWorld.getX(), the loop is complete.
Example
public int swan() { int count = 10, blah = 10; while(count>0) { count --; //count = count-1; blah += count; //blah = blah + count; } return blah; }Explanation of ++, --, etcIn Code:
++ (adds 1)
-- (subtracts 1)
+= (adds count to blah)
-= (subtracts from blah)
*= (multiplies to blah)
/= (divides by blah)
Difference between loops & if statements:
-if statements repeat only ONCE
-loops keep going until it is false
-be very careful when using loops. If you are not, you can easily create an endless loop that will keep on working and never be false and could very easily crash your computer.
In a for or while loop, the condition is evaluated at the beginning of the loop and the program does not go inside the loop if the condition is false. Thus, the body of the loop may be skipped entirely if the condition is false at the very beginning.